module(..., package.seeall)

require "utils.misc"
require "utils.oop"
require "utils.list"
require "utils.os"

local FS = MOAIFileSystem

----------------------------------------------------------------------------------------------------
--[[ A basic resource finder. Keeps a list of directories to search for resources, and can later be
queried about those resources. 
--]]
----------------------------------------------------------------------------------------------------
ResourceFinder = class("ResourceFinder")
ResourceFinder.__init = {path = list(FS.getAbsoluteDirectoryPath("./"))}

function ResourceFinder.add_path(self, directory, index)
    local directory = FS.getAbsoluteDirectoryPath(directory)
    self.path:insert(index or (#self.path + 1), directory)
    return directory
end

function ResourceFinder.find(self, resource)
    local filepath
    for dir in iter(self.path) do
        local path = os.path.join(dir, resource)
        printf("searching %s...", path)
        if FS.checkFileExists(path) then
            filepath = path
            print "Found!"
            break
        end
    end
    if filepath == nil then 
        error(string.format("unable to find resource %s", resource), 2) 
    end
    return filepath
end
